﻿using DrugStore.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DrugStore.Dal;
using DrugStore.Common;

namespace DrugStore.Controllers
{
    public class DrugController : Controller
    {
        //
        // GET: /Drug/

        public ActionResult Index()
        {
            DrugModel drug = new DrugModel();
            List<UnitModel> unitList = new List<UnitModel>();
            using (var context = new DrugStoreEntities())
            {
                //var L2EQuery = context.Units.where(s => s.StudentName == “Bill”);
                System.Data.Objects.ObjectResult<unitSelectAll_Result> unitListDB = context.unitSelectAll();

                foreach (unitSelectAll_Result unit in unitListDB)
                {
                    UnitModel unitModel = new UnitModel();
                    CommonWeb.DuckCopyShallow(unitModel, unit);
                    unitList.Add(unitModel);
                }
                context.Dispose();
            }
            drug.unitList = new SelectList(unitList, "id", "name");

            List<SelectListItem> selectListItemList = new List<SelectListItem>() {
                new SelectListItem(){ Value="1", Text="1"},
                new SelectListItem(){ Value="2", Text="2"},
                new SelectListItem(){ Value="3", Text="3"},
                new SelectListItem(){ Value="4", Text="4"},
                new SelectListItem(){ Value="5", Text="5"},
                new SelectListItem(){ Value="6", Text="6"},
                new SelectListItem(){ Value="7", Text="7"},
                new SelectListItem(){ Value="8", Text="8"},
                new SelectListItem(){ Value="9", Text="9"},
                new SelectListItem(){ Value="10", Text="10"},
            };

            drug.numberOfDayList = new SelectList(selectListItemList, "Value", "Text");

            // get list for Drug
            using (var context = new DrugStoreEntities())
            {
                drug.KeySearch = "";
                drug.countDrug = context.drugCountPaging("").FirstOrDefault().Value;
                drug.CurrentPage = 1;
                if (drug.countDrug > 0)
                {
                    drug.drugList = new List<drugSelectPaging_Result2>();
                    //var L2EQuery = context.Units.where(s => s.StudentName == “Bill”);
                    System.Data.Objects.ObjectResult<drugSelectPaging_Result2> drugListDB = context.drugSelectPaging(1, "", 15);

                    foreach (drugSelectPaging_Result2 drugdb in drugListDB)
                    {
                        drug.drugList.Add(drugdb);
                    }
                
                }
                context.Dispose();
            }
           
            return View(drug);
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult InsertDrug(DrugModel drugModel)
        {
            // id khac khong la update
            if (drugModel.id != 0)
            {
                using (var context = new DrugStoreEntities())
                {
                    var checkId = context.drugSelectByID(drugModel.id).ToList();
                    if (checkId.Count > 0) {
                        int intUpdate = context.drugUpdateByID(drugModel.id, drugModel.code, drugModel.unit_id, drugModel.name, drugModel.manual, drugModel.number_of_day);

                    }
                    context.Dispose();
                }

            } else {
                Drug drug = new Drug();
                CommonWeb.DuckCopyShallow(drug, drugModel);
                using (var context = new DrugStoreEntities())
                {
                    context.Drugs.Add(drug);
                    context.SaveChanges();
                    context.Dispose();
                }
            }
            return RedirectToAction("Index", "Drug");
        }

        [HttpGet]
        public ActionResult PagingDrug(int? PageSize, int? page, string KeySearch)
        {
            DrugModel drug = new DrugModel();
            drug.CurrentPage = page.HasValue ? page.Value : 1;
            // get list for Drug
            using (var context = new DrugStoreEntities())
            {
                drug.KeySearch = KeySearch;
                drug.countDrug = context.drugCountPaging("").FirstOrDefault().Value;
                if (drug.countDrug > 0)
                {
                    drug.drugList = new List<drugSelectPaging_Result2>();
                    //var L2EQuery = context.Units.where(s => s.StudentName == “Bill”);
                    System.Data.Objects.ObjectResult<drugSelectPaging_Result2> drugListDB = context.drugSelectPaging(drug.CurrentPage, KeySearch, PageSize.HasValue ? PageSize.Value : 15);

                    foreach (drugSelectPaging_Result2 drugdb in drugListDB)
                    {
                        drug.drugList.Add(drugdb);
                    }

                }
                context.Dispose();
            }

            return PartialView("_PagingDrug", drug);
        }



        [HttpPost]
        public JsonResult CheckNameOfDrug(String  name)
        {
            if (!"".Equals(name))
            {
                using (var context = new DrugStoreEntities())
                {
                    var drugListDB = context.drugSelectByName(name).ToList();
                    context.Dispose();
                    if (drugListDB.Count > 0 )
                    {
                        JsonResult jsonReturn = Json(new
                        {
                            Status = "Success"
                        }, JsonRequestBehavior.AllowGet);
                        return jsonReturn;
                    }
                }
            }

            JsonResult jsonReturnError = Json(new
            {
                Status = "Error"
            }, JsonRequestBehavior.AllowGet);
            return jsonReturnError;
        }

        [HttpPost]
        public JsonResult CheckCodeOfDrug(String code)
        {
            if (!"".Equals(code))
            {
                using (var context = new DrugStoreEntities())
                {
                    var drugListDB = context.drugSelectByCode(code).ToList();
                    context.Dispose();
                    if (drugListDB.Count > 0)
                    {
                        JsonResult jsonReturn = Json(new
                        {
                            Status = "Success"
                        }, JsonRequestBehavior.AllowGet);
                        return jsonReturn;
                    }
                }
            }

            JsonResult jsonReturnError = Json(new
            {
                Status = "Error"
            }, JsonRequestBehavior.AllowGet);
            return jsonReturnError;
        }
    }
}
